﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_NonWorkingDays : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private INonWorkingDayBLL nwdBLL = new NonWorkingDayBLL();
        private NonWorkingJobSiteBLL nwjBLL = new NonWorkingJobSiteBLL();
        private Main main = new Main();
        private IJobSiteBLL jsBLL = new JobSiteBLL();

        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DataItem != null)
            {
                FillDataYear();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblNwdSeq.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    cmbBGYear.SelectedValue = DataBinder.Eval(DataItem, "BudgetYear").ToString();
                    txtNwdDate.SetValue(DataBinder.Eval(DataItem, "Date"));
                    txtNwdName.Text = Convert.ToString(DataBinder.Eval(DataItem, "Name"));                    
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                }
                else
                {
                    string bgYear1 = Convert.ToString(Session["BudgetYear"]);
                    if (string.IsNullOrEmpty(bgYear1))
                    {
                        bgYear1 = DateTime.Now.AddYears(DateTime.Now.Month < 10 ? -1 : 0).ToString("yyyy", new CultureInfo("th-TH"));
                    }
                    cmbBGYear.SelectedValue = bgYear1;
                    try
                    {
                        var bgYear = Convert.ToInt32(Session["BudgetYear"]);
                        var bgMonth = Convert.ToInt32(string.IsNullOrEmpty(Convert.ToString(Session["Month"])) ? "1": Session["Month"]);
                        var realYear = (bgMonth < 10 ? bgYear + 1 : bgYear) - 543;
                        txtNwdDate.SetValue(new DateTime(realYear, bgMonth, 1));

                        DateTime firstDateOfBgYear = new DateTime(SelectedBgYear - 543, 10, 1);
                        DateTime lastDateOfBgYear = new DateTime(SelectedBgYear - 542, 9, 30);
                        lblMessageDateRangeNotice.Text = "กรุณาเลือกวันในช่วงระหว่าง " +
                                         firstDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH")) +
                                         " - " +
                                         lastDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                         
                    }
                    catch (Exception ex)
                    {
                        var xy = ex.Message;
                    }
                }
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected void FillDataYear()
        {
            int nYear = Int32.Parse(DateTime.Today.Year.ToString()) + 543;

            for (int i = nYear - 10; i <= nYear + 10; i++)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = i.ToString();
                item.Value = i.ToString("0000");
                cmbBGYear.Items.Add(item);
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            if (!Validate()) return;

            var nonWorkingSeq = nwdBLL.Insert(Int32.Parse(cmbBGYear.SelectedValue)
                , DateTime.Parse(txtNwdDate.GetStringValue())
                , txtNwdName.Text.Trim()
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);

            var jobSiteSeqs = (from js in jsBLL.ListJobSiteByTypes("2", "3") select js.Sequence).ToList();
            foreach (var jobSiteSeq in jobSiteSeqs)
            {
                nwjBLL.Insert(nonWorkingSeq, jobSiteSeq.ToString(), "", ssMng.EmpSeqI);
            }

            main.MessageAlert(Page, "[สำเร็จ!] บันทึกการสร้างวันหยุดใหม่เรียบร้อยแล้ว และเพิ่มหน่วยงานที่มีผล " + jobSiteSeqs.Count + " หน่วยงาน (ท่านสามารถเลือกลบออกได้หากไม่ต้องการให้มีผลทุกหน่วยงาน)");
        }
        

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!Validate()) return;

            nwdBLL.Update(Int32.Parse(lblNwdSeq.Text)
                , Int32.Parse(cmbBGYear.SelectedValue)
                , DateTime.Parse(txtNwdDate.GetStringValue())
                , txtNwdName.Text.Trim()
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
            main.MessageAlert(Page, "[สำเร็จ!] บันทึกการแก้ไขวันหยุดใหม่เรียบร้อยแล้ว"); 
        }

        private bool Validate()
        {
            if (string.IsNullOrEmpty((txtNwdName.Text ?? "").Trim()))
            {
                main.MessageAlert(Page, "[ข้อมูลไม่ถูกต้อง!] กรุณากรอกชื่อวันหยุด");
                return false;
            }

            if (txtNwdDate.GetStringValue().Length <= 0)
            {
                main.MessageAlert(Page, "[ข้อมูลไม่ถูกต้อง!] กรุณาเลือกวัน");
                return false;
            }

            DateTime firstDateOfBgYear = new DateTime(SelectedBgYear - 543, 10, 1);
            DateTime lastDateOfBgYear = new DateTime(SelectedBgYear - 542, 9, 30);
            if (txtNwdDate.GetDateValue() < firstDateOfBgYear || txtNwdDate.GetDateValue() > lastDateOfBgYear)
            {
                string message = "สำหรับปีงบประมาณ " + SelectedBgYear + ", กรุณาเลือกวันในช่วงระหว่าง " +
                                 firstDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH")) +
                                 " - " +
                                 lastDateOfBgYear.ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                main.MessageAlert(Page, message);
                return false;
            }

            var nwdInfo = nwdBLL.GetAllNonWorkingDays(SelectedBgYear.ToString())
                .FirstOrDefault(o => o.RecordStatus != "D" && o.Date.HasValue && o.Date.Value.Date == txtNwdDate.GetDateValue());
            if (nwdInfo != null)
            {
                string message = "[ข้อมูลไม่ถูกต้อง!] ท่านเคยสร้างวันหยุดเดียวกันนี้ไว้แล้วชื่อ " + nwdInfo.Name;
                main.MessageAlert(Page, message);
                return false;
            }
            return true;
        }

        private int SelectedBgYear
        {
            get
            {

                if (string.IsNullOrEmpty(Convert.ToString(Request["BudgetYear"])))
                {
                    return Convert.ToInt32(DateTime.Now.AddYears(DateTime.Now.Month < 10 ? -1 : 0).ToString("yyyy", new CultureInfo("th-TH")));
                }
                else
                {
                    return Convert.ToInt32(Request["BudgetYear"]);
                }
            }
        }

    }
}